// pages/infoInput/infoInput.js
const app = getApp()

import {  getServerCategory,getSetmealByServer,gonextpasswordcheck } from '../../request/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language:"zh",
    serverCategory:[],
    selectLanguage:[{"text":"中文","language":"zh"},{"text":"English","language":"en"}],
    weeks:[{week:"0周"},{week:"1周"},{week:"2周"},{week:"3周"},{week:"4周"},{week:"5周"},{week:"6周"},{week:"7周"},{week:"8周"},{week:"9周"},{week:"10周"},{week:"11周"},{week:"12周"},{week:"13周"},{week:"14周"},{week:"15周"},{week:"16周"},{week:"17周"},{week:"18周"},{week:"19周"},{week:"20周"},{week:"21周"},{week:"22周"},{week:"23周"},{week:"24周"},{week:"25周"},{week:"26周"},{week:"27周"},{week:"28周"},{week:"29周"},{week:"30周"},{week:"31周"},{week:"32周"},{week:"33周"},{week:"34周"},{week:"35周"},{week:"36周"},{week:"37周"},{week:"38周"},{week:"39周"},{week:"40周"}],
    // servers:[{title:"产检",id:1},{title:"顺产",id:2},{title:"剖腹产",id:3},{title:"疫苗",id:4}],
    setMeals:[],
    text1:"中文",
    text2:"中文",
    text3:"English",
    filterHide:false,
    serverHide:false,
    setMealHide:false,
    weekHide:false,
    selectedserver:"请选择",
    selectedsetMeal:"请选择",
    // selectedserver:"顺产",
    // selectedsetMeal:"顺产行政+产检基础",
    weektext:"10周",
    selectedweek:10,
    name:"",
    selectedsetMealIndex:undefined,
    selectedserverindex:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this
    // this.data.language = app.globalData.language
    getServerCategory({}).then((res)=>{
      if(res.code == 200){
        if(that.data.serverCategory.length==0){
          res.data.forEach(e=>{e.checked = false})
          res.data[0].checked = true
          console.log(res.data)
          that.setData({serverCategory:res.data,selectedserver:res.data[0].name})
          getSetmealByServer({server:this.data.serverCategory[0]._id}).then((res)=>{
            res.data[0].checked = true
            // console.log(res.data)
            that.setData({setMeals:res.data,selectedsetMealIndex:0,selectedsetMeal:res.data[0].name})
          })
          that.setData({
            selectedserver:this.data.serverCategory[0].name
          })
        }
      }
    }),
    // this.data.serverCategory.forEach(e=>{e.checked=false})
    this.setData({
      // name:"",
      // selectedweek:"10周",
      // weektext:10,
      // language:"zh",
      // text1:"中文",
      // // serverCategory:this.data.serverCategory,
      // setMeals:[],
      password:""
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selectlanguage(e){
    this.setData({filterHide:true})
  },

  dismiss : function(){
    this.setData({
      filterHide : false,
      serverHide :false,
      setMealHide:false
    })
  },
  select(e){
    let str = this.data.selectLanguage[e.currentTarget.dataset.index].text
    this.setData({filterHide:false,language:e.currentTarget.dataset.value,text1:str})
    app.globalData.language = e.currentTarget.dataset.value
  },
  serverselect(e){
    // console.log('aaa')
    this.setData({serverHide:true})
  },
  setMealselect(e){
    this.setData({setMealHide:true})
  },
  clickoneserver(e){
    let index = e.currentTarget.dataset.index
    getSetmealByServer({server:this.data.serverCategory[index]._id}).then((res)=>{
      // console.log(res.data)
      this.setData({setMeals:res.data,selectedsetMeal:"请选择"})
    })
    this.setData({
      selectedserver:this.data.serverCategory[index].name,
      serverHide:false,
    })
  },
  password(e){
    this.setData({password:e.detail.value})
  },

  gonext(e){
    const that = this
    if(this.data.name==""){
      wx.showToast({
        title: '请输入姓名',
        icon:'error'
      })
      return
    }

    if(this.data.selectedserver=="产检"){
      console.log(this.data.selectedweek,this.data.selectedsetMealIndex)
      if(this.data.selectedsetMealIndex==2&&this.data.selectedweek>30){
        wx:wx.showToast({
          title: '请选择其它孕周',
          icon:'error'
        })
        return
      }else if(this.data.selectedsetMealIndex==3&&this.data.selectedweek<16){
        wx:wx.showToast({
          title: '请选择其它孕周',
          icon:'error'
        })
        return
      }
      wx.navigateTo({
        url: '/pages/detail/detail?name=' + this.data.name + '&selectedweek=' + this.data.selectedweek + '&language=' + this.data.language + '&selectedserver=' + this.data.selectedserver + '&selectedsetMeal=' + this.data.selectedsetMeal + '&selectedsetMealIndex=' + this.data.selectedsetMealIndex
      })
    }else if(this.data.selectedserver=="顺产"){
      wx.navigateTo({
        url: '/pages/fertility/fertility?name=' + this.data.name + '&selectedweek=' + this.data.selectedweek + '&language=' + this.data.language + '&selectedserver=' + this.data.selectedserver + '&selectedsetMeal=' + this.data.selectedsetMeal + '&selectedsetMealIndex=' + this.data.selectedsetMealIndex
      })
    }else if(this.data.selectedserver=="剖腹产"){
      wx.navigateTo({
        url: '/pages/fertility/fertility?name=' + this.data.name + '&selectedweek=' + this.data.selectedweek + '&language=' + this.data.language + '&selectedserver=' + this.data.selectedserver + '&selectedsetMeal=' + this.data.selectedsetMeal + '&selectedsetMealIndex=' + this.data.selectedsetMealIndex
      })
    }else if(this.data.selectedserver=="疫苗"){
      let setMeatlIndex= that.data.setMeals[this.data.selectedsetMealIndex].index
      console.log(setMeatlIndex)
      wx.navigateTo({
        url: '/pages/vaccine/vaccine?name=' + this.data.name + '&selectedweek=' + this.data.selectedweek + '&language=' + this.data.language + '&selectedserver=' + this.data.selectedserver + '&selectedsetMeal=' + this.data.selectedsetMeal + '&selectedsetMealIndex=' + setMeatlIndex
      })
    }else if(this.data.selectedserver=="产后"){
      wx.navigateTo({
        url: '/pages/postnatal/postnatal?name=' + this.data.name + '&selectedweek=' + this.data.selectedweek + '&language=' + this.data.language + '&selectedserver=' + this.data.selectedserver + '&selectedsetMeal=' + this.data.selectedsetMeal + '&selectedsetMealIndex=' + this.data.selectedsetMealIndex
      })
    }



    // console.log(this.data.name,this.data.selectedweek,this.data.language,this.data.selectedserver,this.data.selectedsetMeal)
    // app.globalData.staffId = "608fa41c3cab75ae2b7aa37e"
    // gonextpasswordcheck({password:this.data.password,staff:app.globalData.staffId}).then((res)=>{
    //   if(res.code == 200){
    //     if(this.data.selectedserver=="产检"){
    //       wx.navigateTo({
    //         url: '/pages/detail/detail?name=' + this.data.name + '&selectedweek=' + this.data.selectedweek + '&language=' + this.data.language + '&selectedserver=' + this.data.selectedserver + '&selectedsetMeal=' + this.data.selectedsetMeal + '&selectedsetMealIndex=' + this.data.selectedsetMealIndex
    //       })
    //     }else if(this.data.selectedserver=="顺产"){
    //       wx.navigateTo({
    //         url: '/pages/fertility/fertility?name=' + this.data.name + '&selectedweek=' + this.data.selectedweek + '&language=' + this.data.language + '&selectedserver=' + this.data.selectedserver + '&selectedsetMeal=' + this.data.selectedsetMeal + '&selectedsetMealIndex=' + this.data.selectedsetMealIndex
    //       })
    //     }else if(this.data.selectedserver=="剖腹产"){
    //       wx.navigateTo({
    //         url: '/pages/fertility/fertility?name=' + this.data.name + '&selectedweek=' + this.data.selectedweek + '&language=' + this.data.language + '&selectedserver=' + this.data.selectedserver + '&selectedsetMeal=' + this.data.selectedsetMeal + '&selectedsetMealIndex=' + this.data.selectedsetMealIndex
    //       })
    //     }else if(this.data.selectedserver=="疫苗"){
    //       wx.navigateTo({
    //         url: '/pages/vaccine/vaccine?name=' + this.data.name + '&selectedweek=' + this.data.selectedweek + '&language=' + this.data.language + '&selectedserver=' + this.data.selectedserver + '&selectedsetMeal=' + this.data.selectedsetMeal + '&selectedsetMealIndex=' + this.data.selectedsetMealIndex
    //       })
    //     }
    //   }else if(res.code == 0){
    //     wx.showToast({
    //       title: '密码错误',
    //       icon:'error',
    //       duration:1000,
    //       mask:true
    //     })
    //   }
    // })
  },
  weekselect(e){
    this.setData({weekHide:true})
  },
  selectweek(e){
  let str = this.data.weeks[e.currentTarget.dataset.index].week + "周"
    this.setData({weektext:str,selectedweek:e.currentTarget.dataset.index,weekHide:false})
  },
  namechange(e){
    this.setData({name:e.detail.value})
  },
  selectSetMeals(e){
    // e.currentTarget.dataset.index
    this.setData({selectedsetMealIndex:e.currentTarget.dataset.index,selectedsetMeal:this.data.setMeals[e.currentTarget.dataset.index].name,setMealHide:false})
  },
  clear(e){
    this.data.serverCategory.forEach(e=>{e.checked=false})
    this.setData({
      name:"",
      selectedweek:"10周",
      weektext:10,
      language:"zh",
      text1:"中文",
      serverCategory:this.data.serverCategory,
      setMeals:[],
      password:""
    })
  },
  weekchange(e){
    let str = this.data.weeks[e.detail.value].week
    this.setData({weektext:str,selectedweek:e.detail.value})
  },
  servechange(e){
    let index = e.detail.value
    getSetmealByServer({server:this.data.serverCategory[index]._id}).then((res)=>{
      // console.log(res.data)
      this.setData({setMeals:res.data,selectedsetMeal:"请选择"})
    })
    this.setData({
      selectedserver:this.data.serverCategory[index].name
    })
  },
  setMealschange(e){
    this.setData({selectedsetMealIndex:e.detail.value,selectedsetMeal:this.data.setMeals[e.detail.value].name})
  },
  serverradiochange(e){
    let index = Number(e.detail.value)
    getSetmealByServer({server:this.data.serverCategory[index]._id}).then((res)=>{
      res.data[0].checked = true
      // console.log(res.data)
      // if(this.data.serverCategory[index].name=="疫苗"){
      //   this.data.selectedsetMealIndex = 3
      // }
      this.setData({setMeals:res.data})
    })
    this.setData({
      selectedserver:this.data.serverCategory[index].name
    })
  },
  setMealradiochange(e){
    let index = Number(e.detail.value)
    this.setData({selectedsetMealIndex:index,selectedsetMeal:this.data.setMeals[index].name})
  },
  clicksSetMeal(e){
    let index = Number(e.currentTarget.dataset.index)
    this.setData({selectedsetMealIndex:index,selectedsetMeal:this.data.setMeals[index].name})
  },
  serverradiochange(e){
    let index = Number(e.currentTarget.dataset.index)
    getSetmealByServer({server:this.data.serverCategory[index]._id}).then((res)=>{
      res.data[0].checked = true
      // console.log(res.data)
      // if(this.data.serverCategory[index].name=="疫苗"){
      //   this.data.selectedsetMealIndex = 3
      // }
      this.setData({
        setMeals:res.data,
        selectedserver:this.data.serverCategory[index].name,
        selectedserverindex:index})
    })
  }
})